package com.scau.controller;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.alibaba.fastjson.JSONObject;
import com.scau.pojo.Section;
import com.scau.service.SectionService;

@RestController
@RequestMapping("/sectionController")
public class SectionController{

	@Autowired
	SectionService sectionService;
	
	@RequestMapping("/queryAllSection")
	public List<Section> query() {
		return sectionService.query();
	}
	
	@RequestMapping("/editSection")
	public int edit(@RequestBody JSONObject jsonObject) throws Exception{
		int result=0;
		try{
			result=sectionService.update(jsonObject);
		}catch(Exception e){
			System.out.println(e.getMessage());
		}
		return result;
	}
	
	@RequestMapping("/getPPTFile")
	public String getPPT(HttpServletRequest request,@RequestParam("filePPT") MultipartFile filePPT) throws Exception{
		System.out.println("test");
		System.out.println(filePPT.getOriginalFilename());
		BufferedOutputStream stream=null;
		Map<String,Object> resultMap=new HashMap<>();
		String filePath="E:\\bishe\\SEWebsite\\src\\main\\webapp\\ppt\\";
		if(!filePPT.isEmpty()){
			try {
				byte[] bytes=filePPT.getBytes();
				String savePath=filePath+filePPT.getOriginalFilename();
				stream=new BufferedOutputStream(new FileOutputStream(new File(savePath)));
				stream.write(bytes);
				stream.close();
				resultMap.put("result","success");
			} catch (Exception e) {
				resultMap.put("result","failure");
				e.printStackTrace();
				return JSONObject.toJSONString(resultMap).toString();
			}
		}else{
			resultMap.put("result","failure");
			return JSONObject.toJSONString(resultMap).toString();
		}
		return JSONObject.toJSONString(resultMap).toString();
	}
	

}
